//
//  Ipay88PayVC.m
//  PHMall
//
//  Created by admin on 2018/4/4.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "Ipay88PayVC.h"
#import "PaymentView.h"
@interface Ipay88PayVC ()<PaymentResultDelegate>{
    Ipay *paymentsdk;
    IpayPayment *requeryPayment; // FOR REQUERY EXAMPLE ONLY, NOT REQUIRED
    PaymentView *pay;
}

@end

@implementation Ipay88PayVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title=@"订单支付";
    
    UIBarButtonItem* leftBar=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarAction)];
    self.navigationItem.leftBarButtonItem=leftBar;
    
    [self ipay88];
   
    // Do any additional setup after loading the view.
}
- (void)paymentSuccess:(NSString *)refNo withTransId:(NSString *)transId withAmount:(NSString *)amount withRemark:(NSString *)remark withAuthCode:(NSString *)authCode {
    NSLog(@"payment success");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"payment success" message:[NSString stringWithFormat:@"refNo=%@\ntransId=%@\namount=%@\nremark=%@\nauthcode=%@",refNo,transId,amount,remark,authCode] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

- (void)paymentFailed:(NSString *)refNo withTransId:(NSString *)transId withAmount:(NSString *)amount withRemark:(NSString *)remark withErrDesc:(NSString *)errDesc{
    NSLog(@"%@",errDesc);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"payment failed" message:[NSString stringWithFormat:@"refNo=%@\ntransId=%@\namount=%@\nremark=%@\nerrDesc=%@",refNo,transId,amount,remark,errDesc] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

- (void)paymentCancelled:(NSString *)refNo withTransId:(NSString *)transId withAmount:(NSString *)amount withRemark:(NSString *)remark withErrDesc:(NSString *)errDesc{
    NSLog(@"payment cancelled");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"payment cancelled" message:[NSString stringWithFormat:@"refNo=%@\ntransId=%@\namount=%@\nremark=%@\nerrDesc=%@",refNo,transId,amount,remark,errDesc] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

- (void)requerySuccess:(NSString *)refNo withMerchantCode:(NSString *)merchantCode withAmount:(NSString *)amount withResult:(NSString *)result {
    NSLog(@"requery success");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"requery success" message:[NSString stringWithFormat:@"refNo=%@\namount=%@\nmerchantCode=%@\nresult=%@",refNo,amount,merchantCode,result] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

- (void)requeryFailed:(NSString *)refNo withMerchantCode:(NSString *)merchantCode withAmount:(NSString *)amount withErrDesc:(NSString *)errDesc {
    NSLog(@"requery failed");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"requery failed" message:[NSString stringWithFormat:@"refNo=%@\namount=%@\nmerchantCode=%@\nerrDesc=%@",refNo,amount,merchantCode,errDesc] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ([alertView.title isEqualToString:@"payment success"]) {
        [paymentsdk requery:requeryPayment];    // For requery, only refNo/merchantCode/amount is needed, the remaining fields can be left empty
    } else {
        //        exit(0);
    }
}

-(void)ipay88{
    
    IpayPayment *payment = [[IpayPayment alloc] init];
    [payment setPaymentId:@"5"];
    [payment setMerchantKey:@"RhbtlnyDjp"];
    [payment setMerchantCode:@"PH00501"];
    [payment setCurrency:@"PHP"];
    [payment setCountry:@"PH"];
    [payment setRefNo:self.payOrder.ordersn];
    [payment setAmount:self.payOrder.goods_amount];//
    [payment setProdDesc:@"admin"];//
    [payment setUserName:@"Tester"];//
    [payment setUserEmail:@"744599466@qq.com"];//
    [payment setUserContact:@"123456789"];//
    [payment setRemark:@"Testing"];//
    [payment setLang:@"ISO-8859-1"];//
    [payment setBackendPostURL:@"http://www.phmall.com.ph/respond.php?sessionID=ccca85a8fa4aa85b8da3b596c2160458&amp;code=ipay88&amp;receive=back&amp;m_uid=1"];//
    /* 1. Setting IpayPayment Object - END */
    //    5237 3622 5925 9081
    //    537746
    requeryPayment = payment;   // FOR REQUERY EXAMPLE ONLY, NOT REQUIRED
    
    /* 2. SDK Initialization - BEGIN */
    
    
    paymentsdk = [[Ipay alloc] init];
    paymentsdk.delegate = self;
    /* 2. SDK Initialization - END */
    
    
    /* 3. Payment Checkout Method - BEGIN */
    UIView *paymentView = [paymentsdk checkout:payment];
    /* 3. Payment Checkout Method - END */
    [self.view addSubview:paymentView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
